Maven Builds
Maven Builds are a standardized way of compiling, testing, packaging, and managing Java projects. Apache Maven uses a Project Object Model (POM) stored in a pom.xml file to define project information, dependencies, plugins, build configuration, and other settings. Maven then uses its build lifecycle and plugins to perform tasks such as compilation, test execution, packaging, verification, installation, and deployment. :contentReference[oaicite:0]{index=0}
In Selenium automation, Maven is widely used to manage Selenium WebDriver, TestNG, JUnit, WebDriverManager, Apache POI, reporting libraries, logging libraries, and other dependencies. It also makes it easier to run automated tests consistently from an IDE, command line, or CI/CD server.
Course Resource: Selenium Training | Register for Course Demo
1. What is Maven?
Maven is a build automation and project management tool primarily used for Java-based projects. It provides a consistent project structure and a standard build process. Maven uses a declarative configuration file called pom.xml instead of requiring developers to write a large number of build scripts manually.
Maven can manage dependencies, compile source code, execute tests, create JAR or WAR packages, generate reports, run plugins, and publish build artifacts.
- Project build automation.
- Dependency management.
- Compilation of Java source code.
- Test execution.
- Packaging of applications.
- Plugin management.
- Report generation.
- Integration with CI/CD systems.
- Support for multi-module projects.
- Standardized project structure.
2. Why is Maven Important in Selenium?
Selenium projects usually depend on several external libraries. Managing those JAR files manually can become difficult as the project grows. Maven allows dependencies to be declared in pom.xml, after which Maven resolves and downloads the required libraries from configured repositories.
For example, a Selenium TestNG framework may require:
- Selenium Java.
- TestNG.
- WebDriverManager.
- Apache POI.
- Extent Reports or another reporting library.
- Logging libraries.
- JSON processing libraries.
Instead of manually downloading every JAR, the project can declare dependencies in the POM and let Maven manage them.
3. Maven Build Flow
The general Maven build process can be represented as:
Developer
|
v
pom.xml
|
v
Maven
|
+---- Dependencies
|
+---- Plugins
|
+---- Build Lifecycle
|
v
Compile
|
v
Test
|
v
Package
|
v
Verify
|
v
Install / Deploy
Maven's build lifecycle defines ordered phases, and plugins provide the actual goals that perform build tasks. :contentReference[oaicite:1]{index=1}
4. What is pom.xml?
pom.xml stands for Project Object Model. It is the fundamental configuration file of a Maven project. It contains project identity, dependencies, plugins, build configuration, properties, profiles, and other project metadata. :contentReference[oaicite:2]{index=2}
A basic Maven POM looks like:
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>selenium-framework</artifactId>
<version>1.0-SNAPSHOT</version>
</project>
5. Maven Coordinates
Every Maven project is identified using coordinates. The most common coordinates are:
| Element | Purpose | Example |
| groupId | Identifies the organization or project group | com.example |
| artifactId | Identifies the project or artifact | selenium-framework |
| version | Identifies the project version | 1.0-SNAPSHOT |
| packaging | Defines output packaging type | jar |
The combination of groupId, artifactId, and version identifies a Maven artifact. :contentReference[oaicite:3]{index=3}
6. groupId
The groupId identifies the organization, company, team, or logical group to which the project belongs.
<groupId>com.mycompany.automation</groupId>
For a Selenium automation framework, a meaningful group ID might be:
<groupId>com.company.selenium</groupId>
7. artifactId
The artifactId identifies the project or generated artifact.
<artifactId>selenium-test-framework</artifactId>
Examples include:
- selenium-framework
- automation-framework
- web-ui-tests
- selenium-testng-project
8. version
The version identifies a particular version of the project.
<version>1.0-SNAPSHOT</version>
SNAPSHOT is commonly used during active development to indicate a development version rather than a finalized release.
9. Packaging
The packaging element specifies the type of artifact Maven should create.
<packaging>jar</packaging>
Common packaging types include:
| Packaging | Purpose |
| jar | Java Archive |
| war | Web Application Archive |
| pom | Parent or aggregator project |
| ear | Enterprise Application Archive |
If packaging is not specified, Maven uses jar as the default packaging for a typical Maven project. :contentReference[oaicite:4]{index=4}
10. Maven Standard Project Structure
Maven encourages a standard project structure so that developers can understand different Maven projects easily.
selenium-framework
|
|-- pom.xml
|
|-- src
| |-- main
| | |-- java
| | |-- resources
| |
| |-- test
| |-- java
| |-- resources
|
|-- target
The standard Maven layout includes locations such as src/main/java, src/test/java, and the generated target directory. :contentReference[oaicite:5]{index=5}
11. src/main/java
The src/main/java directory generally contains production Java source code.
In a Selenium framework, reusable framework components may be placed here, depending on the project's architecture.
src
|
|-- main
|
|-- java
|
|-- utilities
|-- pages
|-- drivers
12. src/test/java
The src/test/java directory generally contains automated test source code.
Selenium TestNG classes are commonly placed here.
src
|
|-- test
|
|-- java
|
|-- tests
|-- LoginTest.java
|-- SearchTest.java
|-- CheckoutTest.java
13. src/test/resources
The src/test/resources directory can contain test-related resources such as:
- TestNG XML files.
- Configuration files.
- Test data.
- JSON files.
- CSV files.
- Properties files.
- Test resources.
14. target Directory
The target directory contains generated build output. Maven creates it during build execution.
Depending on the build, it can contain:
- Compiled classes.
- Test classes.
- Generated reports.
- Packaged JAR files.
- Temporary build files.
- Other generated artifacts.
The target directory is generated output and normally should not be committed to source control.
15. Maven Dependencies
A dependency is an external library required by the project. Dependencies are declared inside the <dependencies> section of pom.xml.
<dependencies>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>VERSION</version>
</dependency>
</dependencies>
Maven resolves the dependency and its required transitive dependencies according to the project's configuration.
16. Selenium Dependency in Maven
A Selenium Maven dependency allows the framework to use Selenium WebDriver classes without manually adding Selenium JAR files to the project.
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>VERSION</version>
</dependency>
Use the Selenium version selected for your project's compatibility requirements rather than blindly copying an outdated version.
17. TestNG Dependency
For a Selenium TestNG framework, TestNG can be declared as a Maven dependency.
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>VERSION</version>
<scope>test</scope>
</dependency>
The test scope indicates that the dependency is intended for test compilation and execution.
18. WebDriverManager Dependency
Some Selenium frameworks use WebDriverManager to simplify browser driver management.
<dependency>
<groupId>io.github.bonigarcia</groupId>
<artifactId>webdrivermanager</artifactId>
<version>VERSION</version>
</dependency>
Example usage:
WebDriverManager.chromedriver().setup();
WebDriver driver = new ChromeDriver();
19. Complete Selenium TestNG pom.xml Example
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>selenium-framework</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>VERSION</version>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>VERSION</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
20. What is Maven Repository?
A Maven repository is a location where Maven artifacts and metadata are stored. Maven can retrieve project dependencies from configured repositories.
Common repository concepts include:
- Local repository.
- Central repository.
- Remote repositories.
- Private organizational repositories.
21. Local Maven Repository
The local repository is stored on the developer's machine. Maven uses it to cache downloaded dependencies and locally installed artifacts.
On many systems, the default location is:
~/.m2/repository
When a required dependency is already available in the local repository, Maven can reuse the cached artifact instead of downloading it again.
22. Maven Central Repository
Maven Central is a major public repository used for distributing Java libraries and Maven artifacts. Maven projects can resolve many commonly used dependencies from repositories configured through Maven's repository mechanisms.
For example, Selenium and TestNG artifacts are commonly obtained through Maven-compatible repositories.
23. Maven Build Lifecycle
Maven organizes project builds through lifecycles. Maven provides three built-in lifecycles:
- default: Handles the main build and deployment process.
- clean: Handles removal of generated build files.
- site: Handles project site/documentation generation.
The Maven documentation describes the default lifecycle as a sequence of phases executed in order up to the phase requested. :contentReference[oaicite:6]{index=6}
24. Important Maven Build Phases
| Phase | Purpose |
| validate | Checks that the project is correct and required information is available. |
| compile | Compiles main source code. |
| test | Runs tests. |
| package | Packages compiled code into the configured format. |
| verify | Performs checks to verify the package and build results. |
| install | Installs the artifact into the local repository. |
| deploy | Publishes the artifact to a remote repository in a suitable build/release environment. |
These phases are part of Maven's default lifecycle and execute sequentially when a later phase is requested. :contentReference[oaicite:7]{index=7}
25. mvn validate
The validate phase checks whether the project is correctly configured and whether the necessary information is available.
mvn validate
It is useful as an early configuration check.
26. mvn compile
The compile phase compiles the project's main source code.
mvn compile
Maven automatically executes the earlier phases required before compile.
27. mvn test
The test phase compiles and executes tests using the configured testing framework.
mvn test
For a Selenium TestNG project, test execution depends on the project's test plugin and configuration.
28. mvn package
The package phase takes compiled code and packages it into the project's configured distributable format, such as a JAR.
mvn package
Because Maven lifecycle phases are sequential, invoking package also runs the earlier required phases.
29. mvn verify
The verify phase performs checks to verify that the package is valid and satisfies configured quality criteria.
mvn verify
Maven's official lifecycle documentation recommends mvn verify when you are unsure which lifecycle phase to invoke for a complete build check. :contentReference[oaicite:8]{index=8}
30. mvn install
The install phase installs the built artifact into the local Maven repository.
mvn install
This makes the artifact available to other Maven projects on the same machine as a local dependency.
31. mvn deploy
The deploy phase publishes the final artifact to a configured remote repository.
mvn deploy
Deploy is generally used in build, integration, or release environments rather than as a normal local development command. :contentReference[oaicite:9]{index=9}
32. mvn clean
The clean lifecycle removes generated files from previous builds.
mvn clean
This is useful when a clean build is required.
33. mvn clean test
A common Selenium automation command is:
mvn clean test
This first removes previous build output and then executes the test lifecycle through the test phase.
34. mvn clean package
This command cleans the previous build and then packages the project.
mvn clean package
It is useful when you want to ensure that the package is created from a fresh build.
35. mvn clean verify
This command performs a clean build and executes the lifecycle through verify.
mvn clean verify
It is useful in automated build environments because it provides a fresh build followed by verification.
36. Maven Plugins
Maven plugins provide the actual goals that perform build tasks. A build phase can have one or more plugin goals bound to it. :contentReference[oaicite:10]{index=10}
Examples include:
- Maven Compiler Plugin.
- Maven Surefire Plugin.
- Maven Failsafe Plugin.
- Maven Clean Plugin.
- Maven JAR Plugin.
- Maven Resources Plugin.
- Maven Dependency Plugin.
37. Plugin Goal vs Build Phase
| Build Phase | Plugin Goal |
| test | surefire:test |
| compile | compiler:compile |
| package | jar:jar for typical JAR packaging |
| clean | clean:clean |
A phase represents a stage in the lifecycle, while a goal represents a specific plugin task. Maven binds plugin goals to phases according to the project configuration and packaging. :contentReference[oaicite:11]{index=11}
38. Maven Surefire Plugin
The Maven Surefire Plugin is commonly used for running unit tests during the Maven test phase. In Selenium TestNG projects, Surefire can be configured to execute TestNG test classes or suites.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>VERSION</version>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>testng.xml</suiteXmlFile>
</suiteXmlFiles>
</configuration>
</plugin>
39. Running TestNG Through Maven
A Selenium TestNG framework can be configured so Maven runs a TestNG XML suite.
<configuration>
<suiteXmlFiles>
<suiteXmlFile>testng.xml</suiteXmlFile>
</suiteXmlFiles>
</configuration>
Then the suite can be executed using:
mvn test
40. TestNG XML with Maven
A simple TestNG suite may look like:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="Selenium Suite">
<test name="Regression Tests">
<classes>
<class name="tests.LoginTest"/>
<class name="tests.SearchTest"/>
</classes>
</test>
</suite>
Maven can be configured to execute this suite through the Surefire plugin.
41. Maven Build and Selenium Test Execution
The combined execution flow can be represented as:
mvn test
|
v
Read pom.xml
|
v
Resolve Dependencies
|
v
Compile Test Code
|
v
Start TestNG
|
v
Execute Selenium Tests
|
v
Generate Test Results
|
v
Build Completed
42. Maven Properties
Maven properties allow reusable values to be defined in the POM.
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
</properties>
Properties can make version and configuration management easier.
43. Dependency Version Property
Dependency versions can also be stored in properties.
<properties>
<testng.version>VERSION</testng.version>
<selenium.version>VERSION</selenium.version>
</properties>
They can then be referenced inside dependency declarations.
<version>${selenium.version}</version>
44. Maven Dependency Scope
Dependency scope controls where a dependency is available during the Maven build.
| Scope | Typical Purpose |
| compile | Available for main compilation and runtime. |
| test | Used for compiling and running tests. |
| provided | Provided by the runtime/container. |
| runtime | Needed at runtime but not necessarily for compilation. |
| system | References a local system dependency and should generally be avoided when possible. |
45. Maven Dependency Management
Dependency management helps control dependency versions and transitive dependency behavior. Larger projects can centralize versions and configuration to reduce duplication.
Maven also resolves transitive dependencies, meaning a dependency may bring in other libraries required by it.
46. Maven and Transitive Dependencies
Suppose dependency A requires dependency B. When Maven resolves dependency A, Maven can also resolve the required transitive dependency B according to dependency resolution rules.
Project
|
v
Dependency A
|
v
Dependency B
|
v
Additional Required Libraries
This reduces the need to manually declare every underlying library.
47. Maven Profiles
Maven profiles allow different build configurations to be activated for different environments or situations.
For example, a Selenium framework might use different configuration values for QA and staging environments.
<profiles>
<profile>
<id>qa</id>
<properties>
<environment>qa</environment>
</properties>
</profile>
<profile>
<id>staging</id>
<properties>
<environment>staging</environment>
</properties>
</profile>
</profiles>
48. Activating a Maven Profile
A profile can be activated from the command line.
mvn test -Pqa
Another profile can be selected using:
mvn test -Pstaging
This can be useful when the same automation framework needs to execute against different environments.
49. Maven and Selenium Environment Configuration
Maven properties and profiles can be combined with Selenium configuration.
Environment
|
+---- QA
|
+---- Staging
|
+---- Production
|
v
Maven Profile
|
v
Test Configuration
|
v
Selenium WebDriver
The actual environment-selection strategy should be designed carefully, especially for production systems.
50. Maven Command-Line Options
Maven supports command-line options that are useful during automation execution.
| Command | Purpose |
| mvn test | Run tests. |
| mvn clean test | Clean and run tests. |
| mvn clean package | Clean and package. |
| mvn clean verify | Clean and verify. |
| mvn install | Install artifact locally. |
| mvn deploy | Deploy artifact to configured remote repository. |
| mvn -Pqa test | Run tests using the QA profile. |
51. Maven Build with System Properties
System properties can be supplied from the command line and can be consumed by the test framework.
mvn test -Dbrowser=chrome
Another example:
mvn test -Dbrowser=firefox -Denvironment=qa
This approach can be useful for CI/CD execution where browser and environment values are supplied dynamically.
52. Maven and Cross-Browser Testing
Maven can be used as the execution layer for a Selenium framework that supports multiple browsers.
mvn test -Dbrowser=chrome
mvn test -Dbrowser=firefox
mvn test -Dbrowser=edge
The Java framework can read the browser property and initialize the appropriate WebDriver.
53. Browser Configuration Example
String browser = System.getProperty("browser", "chrome");
if (browser.equalsIgnoreCase("chrome")) {
driver = new ChromeDriver();
} else if (browser.equalsIgnoreCase("firefox")) {
driver = new FirefoxDriver();
} else if (browser.equalsIgnoreCase("edge")) {
driver = new EdgeDriver();
} else {
throw new IllegalArgumentException(
"Unsupported browser: " + browser
);
}
54. Maven and Page Object Model
Maven works well with the Page Object Model because Maven manages dependencies and builds the project while POM organizes Selenium page interaction logic.
src
|
|-- main
| |-- java
| |-- pages
| |-- LoginPage.java
| |-- HomePage.java
|
|-- test
| |-- java
| |-- tests
| |-- LoginTest.java
|
|-- pom.xml
55. Maven Build with POM and TestNG
A typical Selenium automation architecture can be:
pom.xml
|
+---- Selenium Dependency
|
+---- TestNG Dependency
|
+---- Plugins
|
v
TestNG
|
v
Test Classes
|
v
Page Objects
|
v
WebDriver
|
v
Application
56. Maven and Test Reports
Maven test execution can produce test-result files that can be consumed by reporting systems and CI/CD tools. Maven's test lifecycle commonly works with test execution plugins such as Surefire, while integration-test setups may use Failsafe.
These results can be used to identify:
- Passed tests.
- Failed tests.
- Skipped tests.
- Execution counts.
- Failure information.
- Build status.
57. Maven and Selenium Screenshots
Maven itself does not take Selenium screenshots. Screenshot functionality belongs to the Selenium automation framework. However, Maven can execute the framework that captures screenshots and can package or expose the generated test artifacts through the build process.
A common flow is:
Selenium Test
|
v
Failure Detected
|
v
Capture Screenshot
|
v
Save Screenshot
|
v
Test Report
|
v
Maven Build Result
58. Maven and CI/CD
Maven is commonly used as a command-line build tool inside CI/CD pipelines. A CI server can check out the project, resolve dependencies, execute Maven commands, run automated tests, and publish build artifacts or reports.
Git Repository
|
v
CI/CD Server
|
v
mvn clean verify
|
v
Compile
|
v
Test
|
v
Verify
|
v
Reports / Artifacts
59. Maven and Jenkins
A Jenkins job can execute Maven commands such as:
mvn clean test
For a more complete build:
mvn clean verify
Jenkins can then process test results and other generated artifacts according to the pipeline configuration.
60. Maven and Git
Maven projects are commonly stored in Git repositories.
Typical workflow:
Developer
|
v
Git Commit
|
v
Push to Repository
|
v
CI Server
|
v
Maven Build
|
v
Automated Tests
|
v
Build Result
Generated directories such as target are normally excluded from source control.
61. Maven Clean Build
A clean build starts by removing previous generated output and then rebuilding the project.
mvn clean verify
This is particularly useful when diagnosing problems that may be caused by stale compiled files or previous build output.
62. Maven Offline Mode
Maven can be run in offline mode when all required dependencies and plugins are already available locally.
mvn -o test
Offline mode can be useful when network access is unavailable, but it cannot download dependencies that are missing from the local repository.
63. Maven Debugging
When a Maven build fails, the console output should be inspected carefully. Maven errors may originate from:
- Invalid POM configuration.
- Dependency resolution problems.
- Compilation errors.
- Test failures.
- Plugin configuration errors.
- Environment configuration issues.
- Java version incompatibility.
- Network or repository problems.
64. Useful Maven Debug Commands
Maven provides command-line options for obtaining more detailed build information.
mvn test -e
For more verbose Maven debugging information:
mvn test -X
The -e option provides additional error information, while -X enables extensive debug output.
65. Common Maven Build Errors
| Error Type | Possible Cause |
| Dependency resolution failure | Incorrect dependency coordinates or repository/network issue. |
| Compilation failure | Java source-code or compatibility problem. |
| Test failure | One or more automated tests failed. |
| Plugin execution failure | Incorrect or incompatible plugin configuration. |
| Missing POM | Maven was executed outside the expected project directory. |
| Java version error | Configured source/target level does not match the available JDK. |
66. Maven Build Failure vs Test Failure
A Maven build can fail because of different reasons. A test failure is different from a compilation or dependency-resolution failure.
Maven Build
|
+---- Dependency Failure
|
+---- Compilation Failure
|
+---- Test Failure
|
+---- Plugin Failure
|
+---- Packaging Failure
When analyzing CI results, it is important to identify which stage actually failed.
67. Maven and Java Version
Maven builds depend on a compatible Java runtime/JDK and project configuration. The Java version used to compile the project should be compatible with the project's source code and dependencies.
java -version
mvn -version
These commands are useful for checking the Java and Maven environments available to the current command-line session.
68. Maven Compiler Configuration
The Maven Compiler Plugin can be configured to compile Java code for a selected Java version.
<properties>
<maven.compiler.release>17</maven.compiler.release>
</properties>
The exact Java release should match the project's requirements and installed JDK.
69. Maven Multi-Module Project
Maven supports multi-module projects where a parent POM manages multiple child modules.
automation-project
|
|-- pom.xml
|
|-- login-tests
| |-- pom.xml
|
|-- search-tests
| |-- pom.xml
|
|-- checkout-tests
|-- pom.xml
This structure can be useful for large automation projects that contain multiple logical test modules.
70. Maven Parent POM
A parent POM can centralize common configuration such as dependency versions, plugin versions, properties, and shared build settings.
<parent>
<groupId>com.example</groupId>
<artifactId>automation-parent</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
This helps reduce duplicate configuration across modules.
71. Maven Build Profiles for Selenium
Profiles can be useful for selecting different Selenium environments.
mvn clean test -Pqa
mvn clean test -Pstaging
The framework can read the active environment and select the appropriate application URL and other non-secret configuration.
72. Maven and External Test Data
Maven can execute Selenium tests that consume external data sources such as:
- Excel files.
- CSV files.
- JSON files.
- Properties files.
- Database records.
- Environment variables.
The data-reading implementation belongs to the automation framework, while Maven provides the build and execution infrastructure.
73. Maven and Data Providers
Maven and TestNG Data Providers solve different problems but work well together.
pom.xml
|
v
Maven
|
v
TestNG
|
v
@DataProvider
|
v
Multiple Test Data Sets
|
v
Selenium Test
Maven starts and manages the build, while TestNG Data Providers supply multiple data sets to the tests.
74. Maven Build with Parallel Testing
Maven can start a TestNG suite that uses parallel execution. The framework must be thread-safe when multiple Selenium tests run concurrently.
mvn test
|
v
TestNG
|
+---- Thread 1 --> Chrome --> Test A
|
+---- Thread 2 --> Firefox --> Test B
|
+---- Thread 3 --> Edge --> Test C
WebDriver instances should not be shared unsafely between concurrent test threads.
75. Maven and Environment Variables
CI/CD pipelines can provide configuration values through environment variables or command-line properties.
mvn test -Dbrowser=chrome -Denvironment=qa
Java code can read these values:
String browser = System.getProperty("browser", "chrome");
String environment = System.getProperty("environment", "qa");
76. Maven Build in a Selenium Framework
A practical Selenium automation framework can use Maven as the central build layer.
selenium-framework
|
|-- pom.xml
|
|-- src
| |
| |-- main
| | |-- java
| | |-- pages
| | |-- utilities
| | |-- drivers
| |
| |-- test
| |-- java
| | |-- tests
| | |-- data
| |
| |-- resources
| |-- testng.xml
| |-- config.properties
|
|-- target
77. Complete Maven Selenium Example
The following example demonstrates a simplified Maven-based Selenium TestNG project.
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
public class LoginTest {
private WebDriver driver;
@BeforeMethod
public void setup() {
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://example.com/login");
}
@Test
public void loginTest() {
driver.findElement(By.id("username"))
.sendKeys("testuser");
driver.findElement(By.id("password"))
.sendKeys("testpass");
driver.findElement(By.id("loginButton"))
.click();
Assert.assertTrue(
driver.getTitle().contains("Dashboard")
);
}
@AfterMethod
public void tearDown() {
if (driver != null) {
driver.quit();
}
}
}
78. Complete Maven Execution Flow
Developer
|
v
Write Selenium Test
|
v
Configure pom.xml
|
v
Add Dependencies
|
v
Run mvn clean test
|
v
Maven Reads POM
|
v
Resolve Dependencies
|
v
Compile Code
|
v
Start TestNG
|
v
Launch Selenium WebDriver
|
v
Execute Tests
|
v
Generate Results
|
v
Build Status
79. Maven Best Practices
- Keep the POM clean and readable.
- Use meaningful groupId and artifactId values.
- Keep dependency versions controlled.
- Avoid unnecessary dependencies.
- Use appropriate dependency scopes.
- Use properties for shared versions where useful.
- Keep generated target files out of source control.
- Use Maven Wrapper when consistent Maven versions are important for the project.
- Use CI/CD to execute repeatable Maven builds.
- Keep environment configuration separate from application interaction logic.
- Avoid storing secrets directly in source-controlled POM files.
- Use clean builds when troubleshooting unexplained build behavior.
- Keep plugin versions explicitly controlled where appropriate.
- Use profiles carefully for environment-specific configuration.
80. Common Mistakes in Maven Builds
- Running Maven outside the project directory containing pom.xml.
- Using invalid dependency coordinates.
- Using incompatible dependency versions.
- Ignoring Java version compatibility.
- Adding unnecessary dependencies.
- Hard-coding credentials in pom.xml.
- Committing the target directory to Git.
- Using outdated plugins without considering compatibility.
- Misconfiguring the Surefire plugin.
- Using the wrong TestNG suite path.
- Confusing Maven lifecycle phases with plugin goals.
- Assuming every Maven build failure is caused by Selenium.
81. Maven Build Troubleshooting Steps
- Check the current project directory.
- Confirm that pom.xml exists.
- Run mvn -version.
- Check the Java version.
- Inspect dependency coordinates.
- Check the Maven console output.
- Run mvn clean test.
- Use -e for additional error information.
- Use -X for detailed Maven debugging.
- Check TestNG and Selenium configuration.
- Check network/repository availability when dependency resolution fails.
- Check CI environment variables and Java configuration.
82. Maven Builds vs Manual JAR Management
| Manual JAR Management | Maven |
| JARs downloaded manually | Dependencies declared in POM |
| Version management can be difficult | Versions can be centrally controlled |
| Transitive dependencies require manual attention | Maven resolves transitive dependencies |
| Build process may vary by developer | Standard lifecycle commands |
| CI setup can require more manual work | Command-line builds are straightforward |
| Project structure can vary | Standard structure is encouraged |
83. Maven vs Ant
| Feature | Maven | Ant |
| Configuration | POM-based | Build-script based |
| Dependency Management | Built-in dependency mechanism | Usually requires additional configuration/tools |
| Build Lifecycle | Standard lifecycle | Task-driven |
| Project Convention | Strong conventions | More flexible |
| Plugin Ecosystem | Extensive | Extensive tasks |
84. Maven vs Gradle
| Feature | Maven | Gradle |
| Configuration | XML POM | Groovy/Kotlin DSL |
| Build Model | Convention and lifecycle oriented | Highly programmable |
| Dependency Management | Yes | Yes |
| Build Lifecycle | Standardized phases | Task-oriented model |
| Learning Approach | Convention-based | DSL/build-script based |
85. Practical Maven Exercises
- Create a new Maven Java project.
- Create a pom.xml file.
- Add Selenium Java as a dependency.
- Add TestNG as a test dependency.
- Create a Selenium LoginTest class.
- Run the test using mvn test.
- Run the project using mvn clean test.
- Configure Maven Surefire for TestNG.
- Create a testng.xml suite.
- Run the TestNG suite through Maven.
- Add browser selection through a Maven system property.
- Add QA and staging Maven profiles.
- Integrate the project with Jenkins.
- Generate and preserve test results in CI.
86. Real-World Selenium Maven Project
selenium-automation
|
|-- pom.xml
|
|-- src
| |-- main
| | |-- java
| | |-- pages
| | | |-- LoginPage.java
| | | |-- SearchPage.java
| | |
| | |-- utilities
| | |-- DriverFactory.java
| | |-- ConfigReader.java
| |
| |-- test
| |-- java
| | |-- tests
| | | |-- LoginTest.java
| | | |-- SearchTest.java
| | |
| | |-- data
| | |-- TestDataProvider.java
| |
| |-- resources
| |-- testng.xml
| |-- config.properties
|
|-- target
87. Real-World Maven CI/CD Architecture
Git Repository
|
v
CI/CD Pipeline
|
v
Checkout Source
|
v
Maven
|
v
mvn clean verify
|
+---- Dependency Resolution
|
+---- Compilation
|
+---- Selenium Tests
|
+---- TestNG
|
+---- Reports
|
v
Build Result
|
+---- Success
|
+---- Failure
88. Interview Questions on Maven Builds
1. What is Maven?
Maven is a build automation and project management tool commonly used for Java projects.
2. What is pom.xml?
pom.xml is the Project Object Model file that defines project configuration, dependencies, plugins, and other Maven metadata.
3. What are Maven coordinates?
The primary coordinates are groupId, artifactId, and version.
4. What is a Maven dependency?
A dependency is an external library required by a project.
5. What is the Maven build lifecycle?
It is the ordered sequence of phases Maven uses to build and manage a project.
6. What is the difference between compile and test?
Compile builds the main source code, while test runs tests after the required compilation phases.
7. What does mvn clean do?
It executes the clean lifecycle and removes generated build output.
8. What does mvn package do?
It builds the project through the package phase and creates the configured artifact.
9. What does mvn install do?
It installs the built artifact into the local Maven repository.
10. What does mvn deploy do?
It publishes the artifact to a configured remote repository.
11. What is Maven Central?
It is a major public repository for Maven-compatible artifacts.
12. What is the local Maven repository?
It is the local cache and repository used by Maven to store downloaded dependencies and locally installed artifacts.
13. What is a Maven plugin?
A Maven plugin provides goals that perform specific build tasks.
14. What is the difference between a goal and a phase?
A phase represents a lifecycle stage, while a goal is a specific task provided by a plugin.
15. Which plugin commonly runs tests in Maven?
The Maven Surefire Plugin is commonly used for test execution during the test phase.
16. Can Maven execute Selenium tests?
Yes. Maven can build and execute Selenium test frameworks when the required Selenium, testing, and plugin configurations are present.
17. Can Maven run TestNG tests?
Yes. Maven can be configured to execute TestNG tests and TestNG XML suites.
18. What is a Maven profile?
A profile is a conditional or environment-specific configuration set that can modify a Maven build.
19. Why is Maven useful in CI/CD?
Maven provides repeatable command-line build and test commands that can be executed by CI/CD servers.
20. What is mvn clean verify?
It cleans previous generated output and executes the Maven lifecycle through the verify phase.
89. Quick Reference Table
| Command/Concept | Purpose |
| pom.xml | Main Maven project configuration file |
| mvn validate | Validate project configuration |
| mvn compile | Compile main source code |
| mvn test | Compile and execute tests |
| mvn package | Package the project |
| mvn verify | Verify the build/package |
| mvn install | Install artifact locally |
| mvn deploy | Deploy artifact to remote repository |
| mvn clean | Remove generated build output |
| mvn clean test | Clean and execute tests |
| mvn clean verify | Clean and perform verification |
| -D | Pass a system property |
| -P | Activate a Maven profile |
| -e | Show additional error information |
| -X | Enable detailed Maven debug output |
90. Learning Roadmap for Maven Builds
- Understand what Maven is.
- Learn the Maven standard project structure.
- Understand pom.xml.
- Learn groupId, artifactId, and version.
- Understand Maven dependencies.
- Learn dependency scopes.
- Understand Maven repositories.
- Learn the clean, default, and site lifecycles.
- Learn important Maven phases.
- Practice mvn clean, mvn test, mvn package, and mvn verify.
- Learn Maven plugins.
- Configure Surefire for TestNG.
- Run Selenium tests through Maven.
- Learn Maven properties and profiles.
- Pass browser and environment values through Maven properties.
- Integrate Maven with Page Object Model.
- Integrate Maven with Data Providers.
- Execute Maven builds in Jenkins or another CI/CD platform.
- Learn Maven troubleshooting.
- Build a complete Selenium Maven automation framework.
91. Summary
Maven is an important build and project management tool for Java-based Selenium automation frameworks. It provides a standardized project structure, dependency management, build lifecycle, plugin system, and command-line execution model.
The central Maven configuration file is pom.xml, which defines project information, dependencies, plugins, properties, profiles, and other build settings. Maven's lifecycle includes phases such as validate, compile, test, package, verify, install, and deploy. :contentReference[oaicite:12]{index=12}
For Selenium automation, Maven can manage Selenium WebDriver and TestNG dependencies, execute automated tests, integrate with Page Object Model and Data Providers, support environment-specific configurations, and provide a repeatable build process for CI/CD systems.
Final Takeaway: Maven makes Selenium automation projects easier to build, maintain, execute, and integrate into professional development and CI/CD workflows by providing a standardized build lifecycle and dependency-management system.
92. Course Resources
Learn more about Selenium automation and professional testing concepts: